package com.beehoo.dcportal.admin.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.beehoo.dcportal.admin.dao.MessageDao;
import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.MessageBean;
import com.beehoo.dcportal.model.RejectBean;

public class MessageDaoImpl extends BaseDao implements MessageDao {

	private static final Logger logger = Logger.getLogger(MessageDaoImpl.class);

	@Override
	public String createMessage(MessageBean messageBean) throws BaseException {
		String messageId = null;
		try {
			messageId = (String) sqlMapClient.insert(
					"admin-message.createMessage", messageBean);
			logger.info("信息管理-添加信息|#[中心编号：" + messageBean.getStreetId()
					+ "],[信息标题：" + messageBean.getMsgTitle()
					+ "],[信息内容：" + messageBean.getMsgContent()
					+ "],[信息类型：" + messageBean.getMsgType()
					+ "],[信息来源：" + messageBean.getMsgSource()
					+ "],[信息状态：" + messageBean.getMsgState()
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("信息管理-添加信息|#[中心编号：" + messageBean.getStreetId()
					+ "],[信息标题：" + messageBean.getMsgTitle()
					+ "],[信息内容：" + messageBean.getMsgContent()
					+ "],[信息类型：" + messageBean.getMsgType()
					+ "],[信息来源：" + messageBean.getMsgSource()
					+ "],[信息状态：" + messageBean.getMsgState()
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.message.create.error");
		}
		return messageId;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MessageBean> queryMessageForList(Map<String, Object> paramMap)
			throws BaseException {
		List<MessageBean> messageLs = null;
		try {
			messageLs = sqlMapClient.queryForList(
					"admin-message.queryMessageForList", paramMap);
			logger.info("信息管理-信息列表查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[信息状态：" + paramMap.get("msgState")
					+ "],[起始时间：" + paramMap.get("beginDate")
					+ "],[结束时间：" + paramMap.get("endDate")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("信息管理-信息列表查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[信息状态：" + paramMap.get("msgState")
					+ "],[起始时间：" + paramMap.get("beginDate")
					+ "],[结束时间：" + paramMap.get("endDate")
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.message.query.error");
		}
		return messageLs;
	}
	

	@Override
	public int queryMessageCount(Map<String, Object> paramMap)
			throws BaseException {
		int ret = 0;
		try {
			ret = (Integer) sqlMapClient.queryForObject(
					"admin-message.queryMessageCount", paramMap);
			logger.info("信息管理-信息列表查询总数|#[中心编号：" + paramMap.get("streetId")
					+ "],[信息状态：" + paramMap.get("msgState")
					+ "],[起始时间：" + paramMap.get("beginDate")
					+ "],[结束时间：" + paramMap.get("endDate")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("信息管理-信息列表查询总数|#[中心编号：" + paramMap.get("streetId")
					+ "],[信息状态：" + paramMap.get("msgState")
					+ "],[起始时间：" + paramMap.get("beginDate")
					+ "],[结束时间：" + paramMap.get("endDate")
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.message.query.error");
		}
		return ret;
	}

	@Override
	public MessageBean queryMessageDetail(Map<String, Object> paramMap)
			throws BaseException {
		MessageBean bean = null;
		try {
			bean = (MessageBean) sqlMapClient.queryForObject(
					"admin-message.queryMessageDetail", paramMap);
			logger.info("信息管理-信息列表查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[信息状态：" + paramMap.get("msgState")
					+ "],[信息编号：" + paramMap.get("msgId")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("信息管理-信息列表查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[信息状态：" + paramMap.get("msgState")
					+ "],[信息编号：" + paramMap.get("msgId")
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.message.query.error");
		}
		return bean;
	}

	@Override
	public int updateMessage(MessageBean messageBean) throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.update("admin-message.updateMessage", messageBean);
			logger.info("信息管理-更新信息|#[中心编号：" + messageBean.getStreetId()
					+ "],[信息标题：" + messageBean.getMsgTitle()
					+ "],[信息内容：" + messageBean.getMsgContent()
					+ "],[信息类型：" + messageBean.getMsgType()
					+ "],[信息来源：" + messageBean.getMsgSource()
					+ "],[信息状态：" + messageBean.getMsgState()
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("信息管理-更新信息|#[中心编号：" + messageBean.getStreetId()
					+ "],[信息标题：" + messageBean.getMsgTitle()
					+ "],[信息内容：" + messageBean.getMsgContent()
					+ "],[信息类型：" + messageBean.getMsgType()
					+ "],[信息来源：" + messageBean.getMsgSource()
					+ "],[信息状态：" + messageBean.getMsgState()
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.message.update.error");
		}
		return ret;
	}

	@Override
	public int delMessage(String msgId) throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.delete("admin-message.delMessage", msgId);
			logger.info("信息管理-删除信息|#[信息编号： " + msgId + "成功!");
		} catch (SQLException e) {
			logger.error("信息管理-删除信息|#[信息编号： " + msgId + "失败!异常："
					+ e.getMessage());
			throw new BaseException(e, "custom.messages.error.message.del.error");
		}
		return ret;
	}

	@Override
	public int auditMessage(MessageBean messageBean) throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.update("admin-message.auditMessage", messageBean);
			logger.info("信息管理-审核信息|#[信息状态：" + messageBean.getMsgState()
					+ "],[审核人编号：" + messageBean.getAuditorId()
					+ "],[审核人名称：" + messageBean.getAuditor()
					+ "],[信息编号：" + messageBean.getMsgId()
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("信息管理-审核信息|#[信息状态：" + messageBean.getMsgState()
					+ "],[审核人编号：" + messageBean.getAuditorId()
					+ "],[审核人名称：" + messageBean.getAuditor()
					+ "],[信息编号：" + messageBean.getMsgId()
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.message.audit.error");
		}
		return ret;
	}

	@Override
	public String createReject(RejectBean rejectBean) throws BaseException {
		String id = null;
		try {
			id = (String) sqlMapClient.insert("admin-message.createReject", rejectBean);
			logger.info("信息管理-审核信息|#[中心编号：" + rejectBean.getStreetId()
					+ "],[信息编号：" + rejectBean.getMsgId()
					+ "],[驳回人编号：" + rejectBean.getRejPid()
					+ "],[驳回人：" + rejectBean.getRejPerson()
					+ "],[驳回原因：" + rejectBean.getRejReason()
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("信息管理-审核信息|#[中心编号：" + rejectBean.getStreetId()
					+ "],[信息编号：" + rejectBean.getMsgId()
					+ "],[驳回人编号：" + rejectBean.getRejPid()
					+ "],[驳回人：" + rejectBean.getRejPerson()
					+ "],[驳回原因：" + rejectBean.getRejReason()
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.message.audit.error");
		}
		return id;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RejectBean> queryRejects(String msgId) throws BaseException {
		List<RejectBean> lsReject = null;
		try {
			lsReject = sqlMapClient.queryForList("admin-message.queryReject", msgId);
			logger.info("信息管理-查询驳回原因|#[信息编号：" + msgId + "],成功!");
		} catch (SQLException e) {
			logger.error("信息管理-查询驳回原因|#[信息编号：" + msgId + "],失败!异常："
					+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.reject.query.error");
		}
		return lsReject;
	}

}
